//
//  BaseCollectionView.m
//  CollectionView-tesk
//
//  Created by Mac on 15/8/1.
//  Copyright (c) 2015年 BV-3G. All rights reserved.
//

#import "BaseCollectionView.h"

@implementation BaseCollectionView
{
    CGFloat oldXoffset;//开始拖拽时的偏移量
}

- (id)initWithFrame:(CGRect)frame {

    //1.设置CollectionView的布局类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置CollectionView的翻转方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
//    //设置CollectionView的单元格大小
//    flowLayout.itemSize = CGSizeMake(self.itemWidth, 400);
//    
//    //设置最小间距
//    flowLayout.minimumInteritemSpacing = 20;
//    flowLayout.minimumLineSpacing = 20;
//    
//    //设置到四边的间距
//    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //2.初始化collectionview
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    
    if (self) {
        
        //无法实现使单元格中心处在屏幕中线
//        self.pagingEnabled = YES;
        
        //3.声明协议
        self.dataSource = self;
        self.delegate = self;
        
        //给当前单元格的位置属性赋一个初值
        self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        
        //4.注册单元格
//        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        
    }

    return self;
    
}


#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataList.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return nil;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake(self.itemWidth, 400);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(10, (kScreenW -self.itemWidth)/2, 10, (kScreenW -self.itemWidth)/2);
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    //获得滚动的偏移量
    CGFloat xOffset = targetContentOffset->x;

    NSInteger index;
    if (oldXoffset < xOffset) {
        
        index = (xOffset + self.itemWidth/2)/self.itemWidth;
    }else {
    
        index = (xOffset - self.itemWidth/2)/self.itemWidth;
    }
    
    if (index >= _dataList.count -1) {
        
        index = 14;
    }
    
    targetContentOffset->x = index *self.itemWidth +index *10;
    
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
    
    //给当前单元格的位置属性重新赋值
    self.currentIndexPath = newIndexPath;
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    oldXoffset = scrollView.contentOffset.x;

}


@end
